$(window).ready(function()
{
    canvas = $("#gameCanvas");
    context = canvas.get(0).getContext("2d");
    canvasWidth = canvas.width();
    canvasHeight = canvas.height();

    var playGame;

    var player;

    var arrowLeft = 37;
    var arrowUp = 38;
    var arrowRight = 39;
    var arrowDown = 40;

    var uiIntro = $("#gameIntro");
    var uiStats = $("#gameStats");
    var uiComplete = $("#gameComplete");
    var uiPlay = $("#gamePlay");
    var uiReset = $(".gameRest");
    var uiScore = $(".gameScore");
    var uiPlaySpeed = $("#playSpeed");
    var uiDaZhao = $("#dazhao");

    var soundBackground = $("#gameSoundBackground").get(0);
    var soundThrust = $("#gameSoundThrust").get(0);

    //定义玩家类
    var Player = function(x,y)
    {
        this.x = x;
        this.y = y;
        this.width = 20;
        this.height = 20;
        this.vX = 0;
        this.vY = 0;
        this.a = 1;
        this.speed = 10;

        this.moveRight = false;
        this.moveLeft = false;
        this.moveUp = false;
        this.moveDown = false;
    }

    //定义子弹类
    var Bullet = function(x,y)
    {
        this.x = x;
        this.y = y;

    }

    function init()
    {
        playGame = false;
        player = new Player(canvasWidth/2 - 10,canvasHeight - 100);


        uiStats.hide();
        uiComplete.hide();

        uiPlay.click(function(e)
        {
            e.preventDefault();
            uiIntro.hide();
            startGame();
        });

        uiReset.click(function(e)
        {
            e.preventDefault();
            uiComplete().hide();
            startGame();
        });


    }

    function startGame()
    {
        uiStats.show();
        if(!playGame)
            playGame = true;

        $(window).keydown(function(e)
        {
            var keyCode = e.keyCode;

            if(keyCode == arrowUp)
                player.moveUp = true;
            else if(keyCode == arrowDown)
                player.moveDown = true;
            else if(keyCode == arrowLeft)
                player.moveLeft = true;
            else if(keyCode == arrowRight)
                player.moveRight = true;


        });

        $(window).keyup(function(e)
        {
           var keyCode = e.keyCode;

            if(keyCode == arrowUp)
                player.moveUp = false;
            else if(keyCode == arrowDown)
                player.moveDown = false;
            else if(keyCode == arrowLeft)
                player.moveLeft = false;
            else if(keyCode == arrowRight)
                player.moveRight = false;
        });

        animate();
    }

    function animate()
    {
        context.clearRect(0,0,canvasWidth,canvasHeight);

        player.vX = 0;
        player.vY = 0;

        if(player.moveRight)
            player.vX = player.speed;
        if(player.moveLeft)
            player.vX = -player.speed;
        if(player.moveUp)
            player.vY = -player.speed;
        if(player.moveDown)
            player.vY = player.speed;

        player.x += player.vX;
        player.y += player.vY;

        if(player.x - 10 < 0)
            player.x = 10;
        else if(player.x + player.height + 10 > canvasWidth )
            player.x = canvasWidth - 10 - player.height;
        if(player.y - 10 < 0)
            player.y = 10;
        else if(player.y + player.width + 10 > canvasHeight)
            player.y = canvasHeight - 10 - player.width;

        context.fillStyle = "red";
        context.fillRect(player.x,player.y,player.width,player.height);

        uiPlaySpeed.html(player.speed);

        if(playGame)
            setTimeout(animate,33);
    }

    init();
}
)